'''
Created on Mar 24, 2014

@author: kieubinh
extract some features:
-

'''
# -*- coding: utf-8 -*-

linkInput = "data\RITE2-UnitTest\input\RITE2_JA_dev_unittest\RITE2_JA_dev_unittest.parsed.KNP.xml"
linkOutput="data\RITE2-UnitTest\\features\\feature1_train.txt"

import os
directory_path = os.getcwd()

import sys
sys.path.append('../ReadData/')
import xml2dict

if __name__ == '__main__':  
    # get root path to ../javn/
    root_path = directory_path[:directory_path.find("javn")+5]
        
    inputFile = root_path+linkInput
    outputFile = root_path+linkOutput
    
    xml = xml2dict.XML2Dict()
    
    doc = xml.parse(inputFile)
    print outputFile
    out = open(outputFile,'w')
    
    #print doc['dataset']['pair'][0]['t1']['value'].encode('utf8'), len(doc['dataset']['pair'][0]['t1']['value'])
    #print doc['dataset']['pair'][0]['t2']['value'].encode('utf8'), len(doc['dataset']['pair'][0]['t2']['value'])
    # extract Cosine similarity of Characters
    for pair in doc['dataset']['pair']:     
        similar_t1=0
        len_t1 = len(pair['t1']['value'])
        for char_t1 in pair['t1']['value']:
            if char_t1 in pair['t2']['value']:
                similar_t1+=1
        similar_t2=0
        len_t2=len((pair['t2']['value']))
        for char_t2 in pair['t2']['value']:
            if char_t2 in pair['t1']['value']:
                similar_t2+=1
        cosine_char=1.0*min(similar_t1,similar_t2)/(len_t1*len_t2)
        #print len_t1, len_t2
        #print pair['label'].value
        if pair['label'].value=='Y':
            label = 1
        else:
            label = 0
        out.write(str(label)+" 1:"+str(cosine_char)+" \n")
    out.close()
    
        
    
    
        